using System;
using System.Linq;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Widget;
using Android.Telephony;

namespace WisdriContactsAndroid
{
    using Android.Views;

    [Activity(Label = "详细信息")]
    public class Detail : Activity
    {
        private PERSON person;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Create your application here
            SetContentView(Resource.Layout.Detail);
            Bundle b = this.Intent.Extras;
            var id = b.GetString("id");

            person = SQLiteHelper.GetPersonData().Single(p => p.ID == id);

            var textViewName = FindViewById<TextView>(Resource.Id.textViewName);
            var textViewId = FindViewById<TextView>(Resource.Id.textViewId);
            var textViewDepartment = FindViewById<TextView>(Resource.Id.textViewDepartment);
            var textViewMobilePhone = FindViewById<TextView>(Resource.Id.textViewMobilePhone);
            var textViewOfficePhone = FindViewById<TextView>(Resource.Id.textViewOfficePhone);
            var textViewPosition = FindViewById<TextView>(Resource.Id.textViewPosition);
            var textViewInnerPhone = FindViewById<TextView>(Resource.Id.textViewInnerPhone);
            var textViewVirtualPhone = FindViewById<TextView>(Resource.Id.textViewVirtualPhone);
            var textViewCarNo = FindViewById<TextView>(Resource.Id.textViewCarNo);
            var buttonCall = FindViewById<Button>(Resource.Id.buttonCall);
            var buttonSMS = FindViewById<Button>(Resource.Id.buttonSMS);
            var buttonEmail = FindViewById<Button>(Resource.Id.buttonEmail);
            var linearLayoutCar = FindViewById<LinearLayout>(Resource.Id.linearLayoutCar);

            var preference = GetSharedPreferences("settings", FileCreationMode.WorldWriteable);
            if (preference.Contains("IsAdmin"))
            {
                var isAdmin = preference.GetBoolean("IsAdmin", false);

                if (isAdmin)
                {
                    linearLayoutCar.Visibility = ViewStates.Visible;
                }
            }


            textViewId.Text = person.ID;
            textViewName.Text = person.NAME;
            textViewDepartment.Text = person.DEPARTMENT;
            textViewMobilePhone.Text = person.MOBILE_PHONE;
            textViewOfficePhone.Text = person.OFFICE_PHONE;
            textViewPosition.Text = person.POSITION;
            textViewInnerPhone.Text = person.INNER_PHONE;
            textViewVirtualPhone.Text = person.VIRTUAL_PHONE;
            textViewCarNo.Text = person.CAR;

            textViewMobilePhone.Click += OpenDial;
            textViewId.Click += SendEmail;
            buttonSMS.Click += SendSMS;
            buttonCall.Click += OpenDial;
            buttonEmail.Click += SendEmail;

        }

        private void OpenDial(object sender, EventArgs e)
        {
            try
            {
                string input = person.MOBILE_PHONE;
                Intent myIntentDial = new Intent(Intent.ActionDial, Android.Net.Uri.Parse("tel:" + input));
                StartActivity(myIntentDial);

            }
            catch (System.Exception ex)
            {
                Toast.MakeText(this, ex.Message, ToastLength.Long);
            }
        }

        private void SendSMS(object sender, EventArgs e)
        {
            try
            {
                string input = person.MOBILE_PHONE;
                Intent intent = new Intent(Intent.ActionSendto, Android.Net.Uri.Parse("smsto:" + input));
                intent.PutExtra("sms_body", string.Empty);
                StartActivity(intent);

            }
            catch (System.Exception ex)
            {
                Toast.MakeText(this, ex.Message, ToastLength.Long);
            }
        }

        private void SendEmail(object sender, EventArgs e)
        {
            try
            {
                string input = person.ID;

                var email = new Intent(Android.Content.Intent.ActionSend);
                email.PutExtra(Android.Content.Intent.ExtraEmail, new string[]
                    {
                        input + "@wisdri.com",
                        input + "@wisdri.com"
                    });
                //email.PutExtra(Android.Content.Intent.ExtraCc, new string[]{ "person3@xamarin.com" });
                email.PutExtra(Android.Content.Intent.ExtraSubject, "Title");
                email.PutExtra(Android.Content.Intent.ExtraText, string.Empty);
                email.SetType("message/rfc822");
                StartActivity(email);

                Intent intent = new Intent(Intent.ActionSendto, Android.Net.Uri.Parse("smsto:" + input));
                intent.PutExtra("sms_body", string.Empty);
                StartActivity(intent);

            }
            catch (System.Exception ex)
            {
                Toast.MakeText(this, ex.Message, ToastLength.Long);
            }
        }
    }
}

